package com.tyf.mybatisFlex.controller;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.tyf.common.utils.R;
import com.tyf.dubbo.model.Student;
import com.tyf.mybatisFlex.mapper.StudentMapper;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Resource
    private StudentMapper studentMapper;

    /**
     * 查询用户列表
     * @return
     */
    @GetMapping("getList")
    public R getList(){
        List<Student> students = studentMapper.selectAll();
        return R.ok(students.toString());
    }

    @GetMapping("getPage")
    public R getPage(Student student){
        Page<Student> paginate = studentMapper.paginate(
                new Page<>(1, 10),
                new QueryWrapper().eq(Student::getName, student.getName())
        );
        return R.ok(paginate.toString());
    }

    /**
     * 新增学生
     * @param student
     * @return
     */
    @PostMapping("/save")
    public R save(@RequestBody Student student){
        int insert = studentMapper.insert(student.addBirthday(),true);
        return R.ok();
    }

    /**
     * 修改学生
     * @param student
     * @return
     */
    @PostMapping("/edit")
    public R edit(@RequestBody Student student){
        int insert = studentMapper.update(student.addBirthday(),true);
        return R.ok();
    }

    /**
     * 删除学生
     * @param student
     * @return
     */
    @PostMapping("/remove")
    public R remove(@RequestBody Student student){
        int i = studentMapper.deleteById(student.getId());
        return R.ok();
    }
}
